package com.android.tiango.baba.act;

import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.image.listener.LoaderListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.entity.ProductModel;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.ImageLoader;
import com.android.tiango.baba.util.SocialUtils;
import com.android.tiango.baba.util.SocialUtils.ShareListener;
import com.nineoldandroids.util.FileUtils;
import com.umeng.socialize.sso.UMSsoHandler;

/**
 * 抢购成功、分享页面
 * @author txg
 * 
 */

public class ActShareBeen extends BaBaseActivity {

	public final static int TYPE_XINLANG = 0;
	public final static int TYPE_QQZONE = 1;
	public final static int TYPE_FRIENDQ = 2;
	public final static int TYPE_TIANGOU = 3;
	private int mType;
	private SocialUtils utils;
	private ProductModel mProductModel;
	private ImageView mIv_sharehead;
	private TextView mTv_share_text;
	private Bitmap bmp;

	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.layout_share2);
		mImageLoader = new ImageLoader(this, R.drawable.baobei_chanpin);
		mImageLoader.setImageloderListener(new LoaderListener() {
			
			@Override
			public void startLoad(ImageView arg0) {
				showWaitingDialog();
			}
			
			@Override
			public void hasLoaded(ImageView arg0, int arg1) {
				
			}
			
			@Override
			public void finishLoad(ImageView arg0, boolean arg1) {
				dismissWaitingDialog();
				View view= getWindow().getDecorView();
				bmp = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
				view.draw(new Canvas(bmp));
//				bmp就是截取的图片了，可通过bmp.compress(CompressFormat.PNG, 100, new FileOutputStream(file));把图片保存为文件。
				startShare(mType);
				
			}
		});
		mIv_sharehead = (ImageView) findViewById(R.id.iv_sharehead);
		mTv_share_text = (TextView) findViewById(R.id.tv_share_text);
		Intent intent = getIntent();
		if (intent.hasExtra(Constants.TYPE)) {
			utils = new SocialUtils();
			mType = intent.getIntExtra(Constants.TYPE, TYPE_XINLANG);
			if (intent.hasExtra(Constants.DATA)) {
				mProductModel = (ProductModel) intent.getSerializableExtra(Constants.DATA);
				mTv_share_text.setText(mProductModel.getProductName());
				try {
					mImageLoader.loadImage(mProductModel.getProductImages().get(0).getImageUrl(),
							mIv_sharehead);
				} catch (Exception e) {
					mIv_sharehead.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.baobei_chanpin));
				}
			}
			if (mType == TYPE_XINLANG) {
			} else if (mType == TYPE_QQZONE) {
			}
		}
		utils.setListener(new ShareListener() {
			
			@Override
			public void onShareOk() {
				setResult(mType);
				finish();
			}
			
			@Override
			public void onShareFail(String msg) {
				setResult(-1);
				finish();
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = utils.mController.getConfig().getSsoHandler(requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}

	private void startShare(int type) {
		switch (type) {
		case TYPE_XINLANG:
			utils.setShareSina(ActShareBeen.this, "新浪商品抢到了", "http://www.baidu.com", "新浪标题",bmp);
			break;
		case TYPE_QQZONE:
			utils.setShareQQZoe(ActShareBeen.this, "QQ空间商品抢到了~", "http://www.baidu.com", "空间标题",bmp);
			break;
		case TYPE_FRIENDQ:
			utils.setShareWeiXin_Circle(ActShareBeen.this, "朋友圈商品抢到了~~~", "http://www.baidu.com",
					"朋友圈标题",bmp);
			break;
		case TYPE_TIANGOU:

			break;
		default:
			break;
		}

	}
}
